/**
 * Created by apple on 16/12/13.
 */
var total = "";
var rows = 10;
var page_exchange = 1;
var data_exchange = new Array();
var exchange_info = "";//左下角的文章信息

window.onload = function () {
    if (storage_admin_id) {
        select_change();

        $('#dataTables-example').dataTable();
        $("#sideNav").click(function () {
            if ($(this).hasClass('closed')) {
                $('.navbar-side').animate({left: '0px'});
                $(this).removeClass('closed');
                $('#page-wrapper').animate({'margin-left': '260px'});

            }
            else {
                $(this).addClass('closed');
                $('.navbar-side').animate({left: '-260px'});
                $('#page-wrapper').animate({'margin-left': '0px'});
            }
        });


    } else {
        window.location.href = "login.html"
    }
};

//兑换列表信息
function exchange() {
    $.ajax({
        type: 'GET',
        url: "http://118.178.59.93:8080/Motocycle/api/v1/exchanges",
        data: data_exchange,
        async: false,
        success: function (data) {
            var obj = JSON.parse(data);
            try {
                if (obj.code == 0) {
                    total = obj.data.total;
                    var exchange_list_total = "";
                    $.each(obj.data.rows, function (i, item) {
                        exchange_list_total += "<tr>" +
                            "<td>" + item.user.nickname + "</td>" +
                            "<td><img src='" + item.user.avatar + "' style='width: 100px'></td>" +
                            "<td>" + item.good_name + "</td>" +
                            "<td>" + item.price + "</td>" +
                            "<td>" + item.count + "</td>" +
                            "<td>" + item.total_price + "</td>";
                        //主题
                        if (item.status == 0) {
                            exchange_list_total += "<td>" +
                                "<span class='operation_button exchange_add' data-id=" + item.exchange_id + " style='background: #337AEB;' >发货</span>" +
                                "</td>"
                        } else {
                            exchange_list_total += "<td>已发货</td>"
                        }
                        if (item.status == 0) {
                            exchange_list_total += "<td></td>"
                        } else {
                            exchange_list_total += "<td>" +
                                "<span class='operation_button exchange_details' data-id=" + item.exchange_id + " style='background: #FF0100;' >查看详情</span>" +
                                "</td>"
                        }
                        exchange_list_total += "</tr>"
                    });
                    $('#dataTables-example tbody').html(exchange_list_total);
                    page_exchange = data_exchange.page;
                }
            } catch (e) {

            }
        }
    })
}

//每页展示的数目不同
function select_change() {
    $('.pageNum').remove();
    $('#exchange_paginate').html('<div class="pageNum"></div>')
    rows = $("#select_change option:selected").val();
    page_exchange = 1;
    data_exchange = {
        page: page_exchange,
        rows: rows
    };
    exchange();
    if (rows > total) {
        exchange_info = "当前第 1 页 , 已展示 " + total + " 条 , 共 " + total + " 条数据";
    } else {
        exchange_info = "当前第 1 页 , 已展示 " + rows + " 条 , 共 " + total + " 条数据";
    }
    $('#exchange_info').html(exchange_info);
    //分页
    var _pagenum = Math.ceil(total / data_exchange.rows);
    if (_pagenum > 1) {
        var end_list = "";
        //需要分页展示
        $('.pageNum').show();
        $('.pageNum').createPage({
            pageCount: _pagenum,
            current: data_exchange.page,
            backFn: function (p) {
                //单击回调方法，p是当前页码
                data_exchange.page = p;
                end_list = p * data_exchange.rows;
                exchange();
                exchange_info = "当前第 " + p + " 页 , 已展示 " + end_list + " 条 , 共 " + total + " 条数据";
                $('#exchange_info').html(exchange_info)
            }
        });
    }
    $('.loading').hide()
}

//弹出"填写发货信息"
$(document).on('click', '.exchange_add', function () {
    var data_id = $(this).attr('data-id');
    $('.exchanges').show();
    $('.exchanges_submit').attr('data-id', data_id);
});

//关闭"填写发货信息"
$(document).on('click', '.exchanges_del', function () {
    layer.msg('您真的要取消吗', {
        time: 0, //不自动关闭
        btn: ['确定', '取消'],
        yes: function (index) {
            layer.close(index);
            $('.exchanges_name').val('');
            $('.exchanges_num').val('');
            $('.exchanges').hide();
        }
    });

});

//确定发货
$(document).on('click', '.exchanges_submit', function () {
    var data_id = $(this).attr('data-id');
    var _name = $('.exchanges_name').val();
    var _num = $('.exchanges_num').val();
    $('.exchanges').hide();
    if (!_name) {
        layer.msg('请输入物流公司!');
        $('.exchanges').show();
    } else if (!_num) {
        layer.msg('请输入物流单号!');
        $('.exchanges').show();
    } else {
        layer.msg('您选择的物流公司为:' + _name + ',物流单号为:' + _num, {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.ajax({
                    type: 'PUT',
                    url: "http://118.178.59.93:8080/Motocycle/api/v1/exchanges",
                    data: {
                        exchange_id: data_id,
                        express_desc: _name,
                        express_num: _num
                    },
                    async: false,
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            layer.msg('发货成功!正在为您刷新页面...');
                            setTimeout(function () {
                                $('.exchanges_name').val('');
                                $('.exchanges_num').val('');
                                exchange();
                            }, 3000)

                        }
                    }
                })
            },
            btn2: function (index) {
                layer.close(index);
                $('.exchanges').show();
            }
        });
    }


});